package zhihui.mobile.presenter.login;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.zgm.utils.SharedPreferencesUtis;
import com.zgm.utils.StringUtils;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;
import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.bean.user.UserInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.login.LoginManager;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.presenter.shop.BaseShopPresenter;
import zhihui.mobile.receiver.ExampleUtil;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.login.LoginActivity;

/**
 * Created by 张公铭 on 2017/6/29 0029.
 */

public class LoginPresenter extends BaseShopPresenter implements ILoginContract.ILoginPresenter
{
    private Logger logger = Logger.getLogger(LoginPresenter.class);
    private Context mContext;
    private ILoginContract.ILogionView logionView;

    @Override
    public void init(IView view, Context context)
    {
        logionView = (ILoginContract.ILogionView) view;
        mContext = context;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initUserInfo()
    {
        UserInfo curUser = (UserInfo) SharedPreferencesUtis.getObjectFromShare(mContext, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE);
        if (curUser != null)
        {
            if (!TextUtils.isEmpty(curUser.getNo()) && !TextUtils.isEmpty(curUser.getPwd()))
            {
                logionView.initData(curUser.getNo(), curUser.getPwd());
            }
        }
    }

    @Override
    public void login(String userName, String password, String code)
    {
        if (checkData(userName, password))
        {
            logionView.showLoading();
            LoginManager.getInstance().login(userName, password, code, JPushInterface.getRegistrationID(mContext), JPushInterface.getRegistrationID(mContext),new IHttpCallBackListener()
            {
                @Override
                public void success(JSONObject result)
                {
                    logionView.hindLoading();
                    logger.debug("login result:" + result.toString());
                    try
                    {
                        String state = result.getString("state");
                        String msg = result.getString("msg");
                        if (state.equals(Config.SUCCESS))
                        {
                            String data = result.getString("data");
                            Gson gson = new Gson();
                            UserInfo userInfo = gson.fromJson(data, UserInfo.class);
                            UserManager.getInstance().setCurUser(userInfo);
                            SharedPreferencesUtis.saveSharedPreference(mContext, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE, userInfo);
                            logionView.goBack(LoginActivity.class);
                        } else
                        {
                            logionView.showMessage(msg);
                        }
                    } catch (JSONException e)
                    {
                        logionView.showMessage("解析数据失败！");
                    }
                }

                @Override
                public void failed(String message)
                {
                    logionView.hindLoading();
                    logionView.showMessage(message);
                }
            });
        }
    }

    @Override
    public void goShopDetailActivity()
    {
        goShopDetails(mContext, ShopManager.getInstance().getCurShop().getSpu().getId(), 0, logionView);
    }

    /**
     * 数据检测
     */
    private boolean checkData(String phone, String password)
    {
        if (TextUtils.isEmpty(phone))
        {
            logionView.showMessage(mContext.getString(R.string.user_null));
            return false;
        } else if (!StringUtils.isNumeric(phone) || phone.length() != 11)
        {
            logionView.showMessage(mContext.getString(R.string.user_error));
            return false;
        } else if (TextUtils.isEmpty(password))
        {
            logionView.showMessage(mContext.getString(R.string.password_null));
            return false;
        } else if (password.length() < 6)
        {
            logionView.showMessage(mContext.getString(R.string.passwrod_error));
            return false;
        }
        return true;
    }

}
